﻿using System;
using System.Collections.Generic;

using EMan.Common.EnterpriseStructure;
using EMan.Common.WorkTasks;
using EMan.DAL.Firebird;

namespace Server.FunctionalTests.Dal.Helpers
{
  public static class TemporaryDatabaseExtensions
  {
    public static bool InsertDepartment( this TemporaryDatabase db, Department department )
    {
      using( var dalc = db.GetDalc<DepartmentDalc>() )
        return dalc.Insert( department );
    }

    public static Department InsertDepartment( this TemporaryDatabase db )
    {
      var department = new Department(Guid.NewGuid().ToString(), "Description");
      using( var dalc = db.GetDalc< DepartmentDalc >() )
        return dalc.Insert( department ) ? department : null;
    }

    public static bool UpdateDepartment(this TemporaryDatabase db, Department department)
    {
      using( var dalc = db.GetDalc< DepartmentDalc >() )
        return dalc.Update( department );
    }

    public  static bool DeleteDepartment(this TemporaryDatabase db, int id)
    {
      using( var dalc = db.GetDalc< DepartmentDalc >() )
        return dalc.Delete( id );
    }

    public static IList< Department > GetDepartmentsList(this TemporaryDatabase db)
    {
      using( var dalc = db.GetDalc< DepartmentDalc >() )
        return dalc.GetList();
    }

    public static Job InsertJob(this TemporaryDatabase db )
    {
      var job = new Job( Guid.NewGuid().ToString() );
      db.InsertJob( job );
      return job;
    }

    public static bool InsertJob( this TemporaryDatabase db, Job job )
    {
      using( var dalc = db.GetDalc< JobDalc >() )
        return dalc.Insert( job );
    }

    public static bool InsertEmployee( this TemporaryDatabase db, Employee employee )
    {
      using( var dalc = db.GetDalc< EmployeeDalc >() )
        return dalc.Insert( employee );
    }

    public static Employee InsertEmployee( this TemporaryDatabase db)
    {
      var employee = new Employee(db.InsertJob(), db.InsertDepartment(), Guid.NewGuid().ToString().Substring( 0, 20 ), "Firstname", "surname", "secondname");
      using( var dalc = db.GetDalc< EmployeeDalc >() )
        dalc.Insert( employee );
      return employee;
    }

    public static WorkTask GetWorkTask( this TemporaryDatabase db, int id)
    {
      using( var dalc = db.GetDalc< WorkTaskDalc >() )
        return dalc.Get( id );
    }

    public static IList<WorkTask> GetWorkTasksByEmployee( this TemporaryDatabase db, int? employeeId)
    {
      using( var dalc = db.GetDalc< WorkTaskDalc >() )
        return dalc.GetListByEmployee( employeeId );
    }

    public static bool InsertWorkTask( this TemporaryDatabase db, WorkTask workTask )
    {
      using( var dalc = db.GetDalc< WorkTaskDalc >() )
        return dalc.Insert( workTask );
    }
  }
}